// Author: Domien Nowicki

#include "ToolboxWidget.h"
#include <QPainter>

ToolboxWidget::ToolboxWidget(QWidget *parent): QWidget(parent), toolType(NONE), emptyTexture(THUMB_WIDTH, THUMB_HEIGHT)
{
	// Setup the empty texture pixmap (white/gray alternating squares)
	SetupEmptyPixmap();

	vboxLayout = new QVBoxLayout();

	vboxLayout->setSpacing(3);
	vboxLayout->setMargin(3);

	setLayout(vboxLayout);

	// Add tool buttons
	cameraTool = AddButton("icons/cameratool.png");
	cameraTool->setStatusTip("Camera tool");
	cameraTool->setToolTip("Camera tool");
	wallTool = AddButton("icons/walltool.png");
	wallTool->setStatusTip("Wall tool");
	wallTool->setToolTip("Wall tool");
	eraserTool = AddButton("icons/erasertool.png");
	eraserTool->setStatusTip("Eraser tool");
	eraserTool->setToolTip("Eraser tool");
	textureTool = CreateButton("icons/texturetool.png");
	textureTool->setStatusTip("Texture tool");
	textureTool->setToolTip("Texture tool");
	textureListButton = CreateButton("icons/rightarrow.png", false);
	textureListButton->setStatusTip("Show texture list");
	textureListButton->setToolTip("Show texture list");


	textureToolLayout = new QHBoxLayout();

	textureToolLayout->addWidget(textureTool);
	textureToolLayout->addWidget(textureListButton);
	vboxLayout->addLayout(textureToolLayout);

	QObject::connect(cameraTool, SIGNAL(clicked()), this, SLOT(slotCameraToolClicked()));
	QObject::connect(wallTool, SIGNAL(clicked()), this, SLOT(slotWallToolClicked()));
	QObject::connect(eraserTool, SIGNAL(clicked()), this, SLOT(slotEraserToolClicked()));
	QObject::connect(textureTool, SIGNAL(clicked()), this, SLOT(slotTextureToolClicked()));
	QObject::connect(textureListButton, SIGNAL(clicked()), this, SLOT(slotTextureListClicked()));

	// Setup the texture select image
    textureImageBox = new QGroupBox(this);
	textureImageBox->setTitle("Texture:");
    textureImageLayout = new QGridLayout(textureImageBox);
    textureImageLayout->setSpacing(5);
    textureImageLayout->setMargin(5);

    textureImageLabel = new QLabel(textureImageBox);
	textureImageLabel->setPixmap(emptyTexture);
    textureImageLabel->setScaledContents(true);

    textureImageLayout->addWidget(textureImageLabel, 0, 0, 1, 1);
	vboxLayout->addWidget(textureImageBox);


	spacerItem = new QSpacerItem(40, 20, QSizePolicy::Minimum, QSizePolicy::Expanding);
	vboxLayout->addItem(spacerItem);
}

ToolboxWidget::~ToolboxWidget()
{
}

QPushButton* ToolboxWidget::AddButton(const QString& iconFilename)
{
	QPushButton *button = CreateButton(iconFilename);

	vboxLayout->addWidget(button);

	return button;
}


QPushButton* ToolboxWidget::CreateButton(const QString& iconFilename, bool checkable)
{
	// Create new tool button
	QPushButton *button = new QPushButton(this);
	QPixmap icon(iconFilename);
	button->setIcon(icon);
	button->setIconSize(QSize( icon.width(), icon.height() ));

	button->setCheckable(checkable);

	return button;
}

enum ToolboxWidget::Tool ToolboxWidget::GetToolType() const
{
	return toolType;
}

void ToolboxWidget::slotCameraToolClicked()
{
	SetToolState(CAMERA);

	emit clickedTool(toolType);
}

void ToolboxWidget::slotWallToolClicked()
{
	SetToolState(WALL);

	emit clickedTool(toolType);
}

void ToolboxWidget::slotEraserToolClicked()
{
	SetToolState(ERASE);

	emit clickedTool(toolType);
}

void ToolboxWidget::slotTextureToolClicked()
{
	SetToolState(TEXTURE);

	emit clickedTool(toolType);
}

void ToolboxWidget::slotTextureListClicked()
{
	SetToolState(TEXTURE);

	emit clickedTool(toolType);
	emit clickedTextureList();
}


void ToolboxWidget::SetupEmptyPixmap()
{
	QPainter painter(&emptyTexture);

	painter.setBrush(Qt::white);
	painter.drawRect(0, 0, 64, 64);

	// Draw alternating white/gray squares
	int even = 1;
	for(unsigned int cy=0; cy<THUMB_WIDTH/SQUARESIZE; cy++)
	{
		even ^= 1;
		for(unsigned int cx=0; cx<THUMB_HEIGHT/SQUARESIZE; cx++)
		{
			QColor color;

			if (even)
			{
				color = Qt::gray;
			}
			else
			{
				color = Qt::white;
			}

			color.setAlpha(130);
			painter.setPen(Qt::NoPen);
			painter.setBrush(color);

			even ^= 1;

			painter.drawRect(cx*SQUARESIZE, cy*SQUARESIZE, SQUARESIZE, SQUARESIZE);
		}
	}

	
	// Draw gray border
	painter.setPen(QPen(Qt::gray));
	painter.drawLine(QLine(0, 0, 0, THUMB_HEIGHT-1));
	painter.drawLine(QLine(0, THUMB_HEIGHT-1, THUMB_WIDTH-1, THUMB_HEIGHT-1));
	painter.drawLine(QLine(THUMB_WIDTH-1, THUMB_HEIGHT-1, THUMB_WIDTH-1, 0));
	painter.drawLine(QLine(THUMB_WIDTH-1, 0, 0, 0));
}

QSize ToolboxWidget::minimumSizeHint() const
{
	return QSize(100, 350);
}

void ToolboxWidget::SetCurrentTexture(const QPixmap& textureThumb)
{
	textureImageLabel->setPixmap(textureThumb);
}

void ToolboxWidget::SetEmptyTexture()
{
	textureImageLabel->setPixmap(emptyTexture);
}

void ToolboxWidget::SetToolState(enum ToolboxWidget::Tool type)
{
	// Update checked/unchecked states for all tool buttons

	cameraTool->setChecked(false);
	wallTool->setChecked(false);
	eraserTool->setChecked(false);
	textureTool->setChecked(false);


	switch(type)
	{
		case CAMERA:
			cameraTool->setChecked(true);
		break;
		case WALL:
			wallTool->setChecked(true);
		break;
		case ERASE:
			eraserTool->setChecked(true);
		break;
		case TEXTURE:
			textureTool->setChecked(true);
		break;
	}

	// And set the new tool
	toolType = type;
}
